package com.dhecp.project.logstics.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.logstics.domain.FbaDispatchDetail;
import com.dhecp.project.logstics.mapper.FbaDispatchDetailMapper;
import com.dhecp.project.logstics.service.FbaDispatchDetailService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 头程发货单 服务层
 * @Author fxz
 * @Date 2022/9/23
 */
@Service
public class FbaDispatchDetailServicelmpl extends BaseModuleServiceImpl implements FbaDispatchDetailService {

    @Autowired
    private FbaDispatchDetailMapper fbaDispatchDetailMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * 查询头程计划单
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<FbaDispatchDetail> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "code asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }
        //原计划单数据
        List<FbaDispatchDetail> queryList = fbaDispatchDetailMapper.select(params);
        //头程计划单下所有箱子数据
        List<FbaDispatchDetail> fbaDispatchDetailList = fbaDispatchDetailMapper.findFbaDispatchDetail(params);
        String codePlanOrder = String.valueOf(params.get("codePlanOrder"));
        if (fbaDispatchDetailList.size() != 0){
            for (FbaDispatchDetail fbaDispatchDetail : queryList){
                List<FbaDispatchDetail> theFirstCase = fbaDispatchDetailList.stream().filter(
                        fbaDispatchDetail2 -> fbaDispatchDetail2.getCode().equals(fbaDispatchDetail.getCode())
//                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getBoxNum():null);
            }
            //TODO 先取第一箱  后期等逻辑处理好在第二箱的数据
            FbaDispatchDetail fbaDispatchDetail;
            for (int i = 0; i < 6; i++) {
                 fbaDispatchDetail = new FbaDispatchDetail();
                List<FbaDispatchDetail> theFirstCase;
                switch (i) {
                    case 0:
                        fbaDispatchDetail.setCommodityCode("箱重(kg)");
                        fbaDispatchDetail.setScrapData("1");
                        theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getWeight():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 1:
                        fbaDispatchDetail.setCommodityCode("箱规长(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getLength():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 2:
                        fbaDispatchDetail.setCommodityCode("箱规宽(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getWidth():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 3:
                        fbaDispatchDetail.setCommodityCode("箱规高(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getHeight():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 4:
                        fbaDispatchDetail.setCommodityCode("抛重(kg)");
                        fbaDispatchDetail.setScrapData("1");
                       theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getWeightVol():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 5:
                        fbaDispatchDetail.setCommodityCode("商品采购价(￡)");
                        fbaDispatchDetail.setScrapData("1");
                        theFirstCase = fbaDispatchDetailList.stream().filter(
                                fbaDispatchDetail2 -> fbaDispatchDetail2.getCodePlanOrder().equals(codePlanOrder)
                                        && fbaDispatchDetail2.getBoxIndex() == 0
                        ).collect(Collectors.toList());
                        fbaDispatchDetail.setTheFirstCase(theFirstCase.size()>0?theFirstCase.get(0).getPurchasePrice():null);
                        queryList.add(fbaDispatchDetail);
                        break;
                }
            }
        } else {
            FbaDispatchDetail fbaDispatchDetail;
            for (int j = 0; j < 6; j++) {
                fbaDispatchDetail = new FbaDispatchDetail();
                switch (j) {
                    case 0:
                        fbaDispatchDetail.setCommodityCode("箱重(kg)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 1:
                        fbaDispatchDetail.setCommodityCode("箱规长(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 2:
                        fbaDispatchDetail.setCommodityCode("箱规宽(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 3:
                        fbaDispatchDetail.setCommodityCode("箱规高(cm)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 4:
                        fbaDispatchDetail.setCommodityCode("抛重(kg)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                    case 5:
                        fbaDispatchDetail.setCommodityCode("商品采购价(￡)");
                        fbaDispatchDetail.setScrapData("1");
                        queryList.add(fbaDispatchDetail);
                        break;
                }
            }
        }
        return queryList;
    }

}
